/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie06.openvoip.server;

import com.rie06.openvoip.client.Client;
import com.rie06.openvoip.client.Conversation;
import com.rie06.openvoip.client.IHM.home.InterfaceClient;
import com.rie06.openvoip.sound.AudioPlayer;
import com.rie06.openvoip.sound.AudioSpecification;
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.UUID;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author clement
 */
public class ServerUDP implements Runnable {

    //Server Info
    private Server server;
    private Client client;
    private InterfaceClient ihm;
    //UDP
    private Integer udpPort;
    private DatagramSocket datagramSocket;
    //Thread
    private Thread thread;
    private static final Logger logger = Logger.getLogger(ServerUDP.class);

    public ServerUDP(Server server) {

        this.server = server;

        //TODO Port Scan
        udpPort = server.getPort();
        /**
         * ************
         */
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        try {
            //        try {
            datagramSocket = new DatagramSocket(udpPort);
            logger.log(Level.INFO, "Server Started on port: " + this.udpPort);

            while (true) {

                //http://stackoverflow.com/questions/3997459/send-and-receive-serialize-object-on-udp-in-java

                //                try {

                int len = 0;
                DatagramPacket packet = null;
                ByteArrayInputStream baos = null;
                ObjectInputStream oos = null;

                byte[] data = new byte[672];
                packet = new DatagramPacket(data, data.length);
                try {
                    datagramSocket.receive(packet);
                } catch (IOException ex) {
                    java.util.logging.Logger.getLogger(ServerUDP.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
                }

                if (data.length == 672) {


                    byte[] conversationID = new byte[36];
                    byte[] userID = new byte[36];
                    byte[] payload_voice = new byte[600];
                    int j = 0;

                    for (int i = 0; i < 36; i++) {
                        conversationID[j++] = data[i];
                    }
                    j = 0;
                    for (int i = 36; i < 72; i++) {
                        userID[j++] = data[i];
                    }
                    j = 0;
                    for (int i = 72; i < 672; i++) {
                        payload_voice[j++] = data[i];
                    }

                    if (server.getClient().getConversationList().containsKey(UUID.fromString(new String(conversationID)))) {
                        Conversation conversation = (Conversation) server.getClient().getConversationList().get(UUID.fromString(new String(conversationID)));

                        conversation.playSound(UUID.fromString(new String(userID)), payload_voice.clone());

                        logger.log(Level.DEBUG, udpPort + " - Receive UDP packet from conversationID: " + conversation.getUuid() + " - From uuid: " + UUID.fromString(new String(userID)));
                    }

                } else {
                    logger.log(Level.DEBUG, udpPort + " Receive bad UPD packet");
                }

            }

        } catch (SocketException ex) {
            java.util.logging.Logger.getLogger(ServerUDP.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public void setIhm(InterfaceClient ihm) {
        this.ihm = ihm;
    }
}
